<template>
  <page-header-wrapper>
    <page-tab-list>
      <div class="table-page-search-box">
        <a-form layout="inline">
          <a-row :gutter="24">
            <a-col :span="8">
              <a-form-item label="小程序">
                <a-select placeholder="请选择" v-model="queryParam.id" @change="getList()">
                  <a-select-option :value="item.id" v-for="item of idList" :key="item.id">
                    {{ item.app_name }}
                  </a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
            <a-col :md="8" :sm="24">
              <a-form-item label="姓名">
                <a-input placeholder="请输入姓名" v-model="queryParam.name" allowClear></a-input>
              </a-form-item>
            </a-col>
            <a-col :md="8" :sm="24">
              <a-form-item label="用户名称">
                <a-input placeholder="请输入用户名称" v-model="queryParam.nickname" allowClear></a-input>
              </a-form-item>
            </a-col>
            <a-col :md="8" :sm="24">
              <a-form-item label="手机号">
                <div class="flex">
                  <a-input class="inp" placeholder="请输入手机号码" v-model="queryParam.mobile" allowClear></a-input>
                  <a-button type="primary" @click="getList()">查询</a-button>
                </div>
              </a-form-item>
            </a-col>
          </a-row>
        </a-form>
      </div>
    </page-tab-list>
    <a-card :border="false">
      <div class="table-operator" style="display: flex; justify-content: space-between">
        <div></div>
        <a-button
          v-if="!hasChannel"
          type="primary"
          @click="$router.push({ name: 'vipCardEdit', query: { id: queryParam.id } })"
          >会员卡设置</a-button
        >
        <a-button
          v-else
          type="primary"
          @click="$router.push({ path: '/saasChannel/alipayVipCardEdit', query: { id: queryParam.id } })"
          >会员卡设置</a-button
        >
      </div>

      <a-table
        size="default"
        :columns="columns"
        :data-source="list"
        :row-key="(record) => record.id"
        :pagination="{
          total: this.total,
          pageSize: this.pageSize,
          showSizeChanger: false,
          onChange: this.tablePageChange,
        }"
      >
        <span slot="content" slot-scope="text, record">
          <div class="content" v-if="record.user">
            <div class="img">
              <a-avatar shape="square" :size="48" icon="user" :src="record.user.avatar_url" />
            </div>

            <div class="txt">
              <h3>{{ record.user.nickname }}</h3>
              <p>{{ record.external_card_no }}</p>
            </div>
          </div>
          <div v-else>暂无</div>
        </span>

        <span slot="alipay" slot-scope="text, record">
          {{ getAlipayName(record.mini_id) }}
        </span>

        <span slot="open_date" slot-scope="text, record">
          {{ record.open_date }}
        </span>

        <span slot="integral" slot-scope="text, record">
          {{ record.user ? record.user.integral : 0 }}
        </span>

        <span slot="money" slot-scope="text, record">
          {{ record.user ? record.user.money : 0 }}
        </span>

        <span slot="action" slot-scope="text, record">
          <a-button type="link" size="small" @click="handleItem('remove', record)">删除</a-button>
        </span>
      </a-table>
    </a-card>
  </page-header-wrapper>
</template>

<script>
import { cardList, cardDel } from '@/api/marketing/activityNew'
import { alipayList } from '@/api/setting/applet'

const columns = [
  {
    title: 'ID',
    dataIndex: 'id',
    width: 100,
  },
  {
    title: '用户信息',
    scopedSlots: {
      customRender: 'content',
    },
    width: 250,
  },
  {
    title: '小程序',
    scopedSlots: {
      customRender: 'alipay',
    },
  },
  {
    title: '姓名',
    dataIndex: 'name',
  },
  {
    title: '电话',
    dataIndex: 'mobile',
  },
  {
    title: '积分',
    scopedSlots: {
      customRender: 'integral',
    },
  },
  {
    title: '余额',
    scopedSlots: {
      customRender: 'money',
    },
  },
  {
    title: '开卡日期',
    dataIndex: 'open_date',
    scopedSlots: {
      customRender: 'open_date',
    },
  },
  {
    title: '操作',
    scopedSlots: {
      customRender: 'action',
    },
  },
]
export default {
  data() {
    return {
      total: 0,
      pageNo: 1,
      pageSize: 10,
      columns,

      queryParam: {
        name: '',
        mobile: '',
        nickname: '',
      },
      list: [],

      selectedRowKeys: [],
      idList: [],
      hasChannel: false,
    }
  },

  mounted() {
    if (this.$route.meta.hasChannel) this.hasChannel = true

    alipayList()
      .then((res) => {
        if (!res.code && res.data && res.data.length) {
          this.idList = res.data
        }
      })
      .catch((err) => {})

    this.getList()
  },

  computed: {
    getStatus() {
      return (status) => {
        let text, color
        switch (status) {
          case '0':
            text = '未审核'
            color = 'rgba(0, 0, 0, 0.25)'
            break

          case '1':
            text = '已通过'
            color = '#65C817'
            break

          case '2':
            text = '已拒绝'
            color = '#FF4D4F'
            break
        }
        return [text, color]
      }
    },

    getAlipayName() {
      return (id) => {
        let item = this.idList.find((el) => el.id === id)
        return item ? item.app_name : '-'
      }
    },
  },

  methods: {
    // 获取数据列表
    async getList(pageNo = 1) {
      const hide = this.$message.loading('加载中', 0)
      let { queryParam, pageSize } = this

      const res = await cardList({
        ...queryParam,
        pageSize,
        pageNo,
      })
      if (res.code == 0) {
        // this.selectedRowKeys = []
        let { totalCount, list: data } = res.data
        this.total = totalCount
        this.list = data

        console.log('data,', data)
      }
      hide()
    },

    // 分页请求
    tablePageChange(e) {
      this.getList(e)
    },

    // // 选中多行
    // onSelectChange(selectedRowKeys) {
    //   this.selectedRowKeys = selectedRowKeys
    // },

    // 点击单个
    handleItem(type, item) {
      switch (type) {
        case 'remove':
          this.remove(item)
          break
      }
    },

    remove({ id }) {
      this.$confirm({
        title: '提示?',
        content: '是否删除？',
        onOk: () => {
          cardDel({
            card_id: id,
            id: this.queryParam.id,
          }).then((res) => {
            if (res.code !== 0) {
              this.$error({
                content: res.msg,
              })
            } else {
              this.$message.success(res.msg)
              this.getList()
            }
          })
        },
        onCancel: () => {},
      })
    },
  },
}
</script>

<style lang="less" scoped>
.searchBox {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  padding: 8px 0 18px 0;
  /deep/ .ant-input-affix-wrapper {
    width: 220px;
    margin-right: 20px;
  }
  .select {
    display: flex;
    align-items: center;
    margin-right: 20px;
  }
}

.content {
  display: flex;
  align-items: center;
  height: 48px;
  max-width: 300px;
  overflow: hidden;

  .txt {
    margin-left: 16px;
    flex: 1;
    overflow: hidden;

    * {
      margin: 0;
      font-size: 14px;
      text-overflow: ellipsis;
      white-space: nowrap;
      overflow: hidden;
    }

    h3 {
      color: rgba(0, 0, 0, 0.85);
    }

    p {
      color: rgba(0, 0, 0, 0.45);
    }
  }
}

.flex {
  .inp {
    margin-right: 16px;
  }
}
</style>
